-
Notifications
You must be signed in to change notification settings - Fork 12
Add pre-commit for better linting #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Greptile OverviewGreptile SummaryThis PR introduces a comprehensive pre-commit configuration to automate code quality checks. The changes include adding pre-commit hooks for linting (flake8, uncrustify), formatting (autopep8, shfmt), copyright validation, YAML validation, whitespace cleanup, and spell checking. The PR also integrates these checks into the GitHub Actions CI workflow and documents the setup process in Contributing.md. Key Changes:
Impact:
Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Dev as Developer
participant Git as Git Commit
participant PreCommit as Pre-commit Framework
participant Hooks as Pre-commit Hooks
participant CI as GitHub Actions
Dev->>Git: git commit -s -m "message"
Git->>PreCommit: Trigger pre-commit hooks
PreCommit->>Hooks: check-yaml
Hooks-->>PreCommit: ✓ YAML files valid
PreCommit->>Hooks: end-of-file-fixer
Hooks-->>PreCommit: ✓ Fixed missing newlines
PreCommit->>Hooks: trailing-whitespace
Hooks-->>PreCommit: ✓ Removed trailing whitespace
PreCommit->>Hooks: autopep8 (Python formatting)
Hooks-->>PreCommit: ✓ Python code formatted
PreCommit->>Hooks: flake8 (Python linting)
Hooks-->>PreCommit: ✓ Python code passes linting
PreCommit->>Hooks: shfmt (Shell formatting)
Hooks-->>PreCommit: ✓ Shell scripts formatted
PreCommit->>Hooks: copyright-required
Hooks-->>PreCommit: ✓ Copyright headers present
PreCommit->>Hooks: uncrustify (C/C++ formatting)
Hooks-->>PreCommit: ✓ C/C++ code formatted
PreCommit->>Hooks: codespell (Spelling check)
Hooks-->>PreCommit: ✓ No spelling errors
Git->>PreCommit: Trigger commit-msg hook
PreCommit->>Hooks: signed-off-by check
Hooks-->>PreCommit: ✓ Commit is signed off
PreCommit-->>Git: All checks passed
Git-->>Dev: Commit created
Dev->>CI: Push to GitHub
CI->>CI: Checkout code
CI->>CI: Install uncrustify + dependencies
CI->>PreCommit: Run pre-commit on all files
PreCommit->>Hooks: Execute all hooks
Hooks-->>PreCommit: ✓ All checks passed
PreCommit-->>CI: Success
CI-->>Dev: ✓ CI passed
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
23 files reviewed, 1 comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
23 files reviewed, 1 comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
23 files reviewed, 4 comments
| should_redraw = ( | ||
| key != - | ||
| 1) or ( | ||
| current_time - | ||
| last_redraw >= redraw_interval) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
syntax: Autopep8 broke this expression by splitting -1 across lines. key != - on one line and 1) on the next is incorrect formatting.
| should_redraw = ( | |
| key != - | |
| 1) or ( | |
| current_time - | |
| last_redraw >= redraw_interval) | |
| should_redraw = (key != -1) or (current_time - last_redraw >= redraw_interval) |
| header = ( | ||
| f'{ | ||
| "Topic Name":<{MAX_NAME_WIDTH}} { | ||
| "Status":<{STATUS_WIDTH}} ' f'{ | ||
| "Pub Rate (Hz)":<{FRAME_RATE_WIDTH}} ' f'{ | ||
| "Latency (ms)":<{REALTIME_DELAY_WIDTH}} { | ||
| "Expected Hz":<12}') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
syntax: F-string formatting is broken. Line breaks inside the f-string placeholders create invalid syntax.
| header = ( | |
| f'{ | |
| "Topic Name":<{MAX_NAME_WIDTH}} { | |
| "Status":<{STATUS_WIDTH}} ' f'{ | |
| "Pub Rate (Hz)":<{FRAME_RATE_WIDTH}} ' f'{ | |
| "Latency (ms)":<{REALTIME_DELAY_WIDTH}} { | |
| "Expected Hz":<12}') | |
| header = (f'{"Topic Name":<{MAX_NAME_WIDTH}} {"Status":<{STATUS_WIDTH}} ' | |
| f'{"Pub Rate (Hz)":<{FRAME_RATE_WIDTH}} ' | |
| f'{"Latency (ms)":<{REALTIME_DELAY_WIDTH}} {"Expected Hz":<12}') |
| topic_set = { | ||
| topic_name for topic_name, | ||
| topic_type in topic_names_and_types} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
syntax: Set comprehension incorrectly split. The comma between topic_name and topic_type should be on the same line.
| topic_set = { | |
| topic_name for topic_name, | |
| topic_type in topic_names_and_types} | |
| topic_set = {topic_name for topic_name, topic_type in topic_names_and_types} |
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
.pre-commit-config.yaml
Outdated
| - id: autopep8 | ||
| args: [-i, -a, -a] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: The -a -a flags make autopep8 very aggressive, causing the broken formatting in ncurses_frontend.py. Consider using just -i or single -a to avoid overly aggressive line breaking.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Signed-off-by: Blake McHale <bmchale@nvidia.com>
Signed-off-by: Blake McHale <bmchale@nvidia.com>
Signed-off-by: Blake McHale <bmchale@nvidia.com>
Signed-off-by: Blake McHale <bmchale@nvidia.com>
Signed-off-by: Blake McHale <bmchale@nvidia.com>
Signed-off-by: Blake McHale <bmchale@nvidia.com>
Signed-off-by: Blake McHale <bmchale@nvidia.com>
8b43cc2 to
ac97d27
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No files reviewed, no comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No files reviewed, no comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No files reviewed, no comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No files reviewed, no comments
Signed-off-by: Blake McHale <bmchale@nvidia.com>
Signed-off-by: Blake McHale <bmchale@nvidia.com>
Signed-off-by: Blake McHale <bmchale@nvidia.com>
Signed-off-by: Blake McHale <bmchale@nvidia.com>
Signed-off-by: Blake McHale <bmchale@nvidia.com>
Signed-off-by: Blake McHale <bmchale@nvidia.com>
Signed-off-by: Blake McHale <bmchale@nvidia.com>
993b9ef to
a016e5c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No files reviewed, no comments
Signed-off-by: Blake McHale <bmchale@nvidia.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3 files reviewed, 3 comments
.github/workflows/ros-tests.yml
Outdated
| - name: Run pre-commit hooks (linting, sign-off check, copyright check, etc.) | ||
| run: | | ||
| apt update && apt install -y pipx git | ||
| pipx install pre-commit | ||
| pipx ensurepath | ||
| source ~/.bashrc | ||
| pre-commit run --all-files --show-diff-on-failure | ||
| shell: bash | ||
| working-directory: greenwave_monitor | ||
| env: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pre-commit runs in wrong directory. Code is checked out to greenwave_monitor/ subdirectory (line 56) but this step doesn't cd into it before running pre-commit.
| - name: Run pre-commit hooks (linting, sign-off check, copyright check, etc.) | |
| run: | | |
| apt update && apt install -y pipx git | |
| pipx install pre-commit | |
| pipx ensurepath | |
| source ~/.bashrc | |
| pre-commit run --all-files --show-diff-on-failure | |
| shell: bash | |
| working-directory: greenwave_monitor | |
| env: | |
| - name: Run pre-commit hooks (linting, sign-off check, copyright check, etc.) | |
| run: | | |
| cd greenwave_monitor | |
| apt update && apt install -y pipx git | |
| pipx install pre-commit | |
| pipx ensurepath | |
| source ~/.bashrc | |
| pre-commit run --all-files --show-diff-on-failure | |
| shell: bash | |
| env: | |
| DEBIAN_FRONTEND: noninteractive |
.github/workflows/ros-tests.yml
Outdated
|
|
||
| - name: Run pre-commit hooks (linting, sign-off check, copyright check, etc.) | ||
| run: | | ||
| apt update && apt install -y pipx git |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uncrustify dependency missing. Pre-commit config uses uncrustify (line 46 of .pre-commit-config.yaml) but it's not installed here. Contributing.md also mentions it's required.
| apt update && apt install -y pipx git | |
| apt update && apt install -y pipx git uncrustify |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No files reviewed, no comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 files reviewed, 2 comments
.github/workflows/ros-tests.yml
Outdated
|
|
||
| - name: Run pre-commit hooks (linting, sign-off check, copyright check, etc.) | ||
| run: | | ||
| apt update && apt install -y pipx git |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uncrustify is required by the pre-commit config (line 46 of .pre-commit-config.yaml) but isn't installed here. Add it to the apt install command.
| apt update && apt install -y pipx git | |
| apt update && apt install -y pipx git uncrustify |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No files reviewed, no comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No files reviewed, no comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No files reviewed, no comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No files reviewed, no comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No files reviewed, no comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No files reviewed, no comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No files reviewed, no comments
Signed-off-by: Blake McHale <bmchale@nvidia.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No files reviewed, no comments
Signed-off-by: Blake McHale <bmchale@nvidia.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 file reviewed, 1 comment
Signed-off-by: Blake McHale <bmchale@nvidia.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
6 files reviewed, no comments
|
Looks good, thanks! Can you please update the Contributing.md to account for the fact that signing is done automatically, and include the apt install and pipx commands there as well. |
That's already done here. Also signing is not done automatically it just errors if you haven't signed. Usual philosophy with sign-offs is you have to actively acknowledge that it is your own work so I kept that same idea. |
Adds a pre-commit config that uses uncrustify and flake8 just like ament. ament isn't used directly since I wanted formatting to occur outside of a ROS environment when making small fixes. Also has other tools like whitespace, newline, copyright, yaml checks, code spelling, shell script formatting, and commit sign off checking.
Useful for fixing most issues with linting before it is pushed. Automatically reformats your commits, fixes copyright year issues, etc.
To run:
NOTE: the sign-off check does not run in CI, only locally because it affects the commit message which is unchangeable at that point